package com.projectsexception.festivales.provider.handlers;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.projectsexception.festivales.provider.model.FeedFestival;
import com.projectsexception.festivales.provider.util.SAXHandlerUtil;

public class FeedsHandler extends DefaultHandler {
    
    private static final String FEED_ELEMENT = "feed";
    private static final String NAME_ATTRIBUTE = "name";
    private static final String LINK_ATTRIBUTE = "link";
    
    private List<FeedFestival> feeds;
    private FeedFestival tmpFeed;

    @Override
    public void startDocument() {
        feeds = new ArrayList<FeedFestival>();
    }
    
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (s.equals(FEED_ELEMENT)) {
            tmpFeed = new FeedFestival();
            tmpFeed.setName(attributes.getValue(NAME_ATTRIBUTE));
            tmpFeed.setLink(attributes.getValue(LINK_ATTRIBUTE));
        }
    }
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (FEED_ELEMENT.equals(s)) {
            feeds.add(tmpFeed);
        }
    }

    public List<FeedFestival> getResult() {
        return feeds;
    }

}
